﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls.Selections
{
    public partial class EmployeeGroupSelection : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadListBoxFiltered.AllowTransfer = true;
            RadListBoxFiltered.AllowTransferDuplicates = false;
            RadListBoxFiltered.EnableDragAndDrop = true;
            RadListBoxFiltered.ButtonSettings.ShowTransferAll = true;

            RadListBoxFiltered.ButtonSettings.Position = ListBoxButtonPosition.Right;

            if (!IsPostBack)
            {
                ShowAll();

            }
        }

        protected void RadComboBoxType_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            switch (RadComboBoxType.SelectedValue)
            {
                case "All": ShowAll(); break;
                case "SBU": ShowSBU(); break;
                case "Group": ShowGroup(); break;
                case "Employee": ShowEmployee(); break;
                default: break;
            }

            RadListBoxFiltered.DataBind();
        }

        private void ShowEmployee()
        {
            HideOthers("Employee");

        }

        private void ShowGroup()
        {
            throw new NotImplementedException();
        }

        private void ShowSBU()
        {
            HideOthers("SBU");

            RadTreeViewSBU.UnselectAllNodes();
            
        }

        private void ShowAll()
        {
            HideOthers("All");

            BSCEntities context = new BSCEntities();


            List<User> users = (from u in context.Users
                                select u).ToList();

            List<User> removedUsers = RemoveAlreadySelectedUsers(users, RadListBoxSelectedEmployees.Items);

            this.RadListBoxFiltered.DataSource = removedUsers;
            this.RadListBoxFiltered.DataValueField = "ID";
            this.RadListBoxFiltered.DataTextField = "Fullname";
            this.RadListBoxFiltered.DataBind();

            //CheckAll();
        }

        private List<User> RemoveAlreadySelectedUsers(List<User> users, RadListBoxItemCollection alreadySelected)
        {
            int i = 0;
            int count = users.Count;

            while(i<count)
            {
                User u = users[i];
                bool removed = false;

                foreach (RadListBoxItem ri in alreadySelected)
                {
                    if (ri.Value == u.ID.ToString())
                    {
                        users.Remove(u);
                        removed = true;
                        break;
                    }
                    
                }

                if (removed == false)
                {
                    ++i;
                }
                else
                {
                    --count;
                }
            }

            return users;
        }

        private void CheckAll()
        {
            foreach (RadListBoxItem i in RadListBoxFiltered.Items)
            {
                i.Checked = true;
            }
        }

        private void HideOthers(string type)
        {
            if (type == "All")
            {
                //LabelAll.Visible = true;
                PlaceHolderSBU.Visible = false;//RadTreeViewSBU.Visible = false; 
                RadComboBoxEmployee.Visible = false;

            }

            if (type == "SBU")
            {
                LabelAll.Visible = false;
                PlaceHolderSBU.Visible = true;//RadTreeViewSBU.Visible = true;
                RadComboBoxEmployee.Visible = false;

                RadListBoxFiltered.Items.Clear();
            }

            if (type == "Employee")
            {
                LabelAll.Visible = false;
                PlaceHolderSBU.Visible = false;// RadTreeViewSBU.Visible = false;
                RadComboBoxEmployee.Visible = true;


                RadListBoxFiltered.Items.Clear();
            }
        }

        protected void RadTreeViewSBU_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            PopulateAccordingSBU();
        }

        private void PopulateAccordingSBU()
        {
            BSCEntities context = new BSCEntities();

            int SBU_ID = Int32.Parse(RadTreeViewSBU.SelectedValue);

            List<User> users = (from u in context.Users
                                where u.SBU_FK == SBU_ID
                                select u).ToList();

            if (CheckBoxInferiorSBUs.Checked)
            {
                AddInferiorSBUs(users, SBU_ID);
            }

            RadListBoxFiltered.DataSource = RemoveAlreadySelectedUsers(users, RadListBoxSelectedEmployees.Items);
            RadListBoxFiltered.DataValueField = "ID";
            RadListBoxFiltered.DataTextField = "Fullname";

            RadListBoxFiltered.DataBind();
            CheckAll();
        }

        private void AddInferiorSBUs(List<User> users, int SBU_ID)
        {
            BSCEntities context = new BSCEntities();

            List<int> sbu_IDs = (from s in context.SBUs
                                 where s.Superior_SBU_FK == SBU_ID
                                 select s.SBU_ID).ToList();

            foreach (int i in sbu_IDs)
            {
                AddUsers(users, i);
                AddInferiorSBUs(users, i);
            }


        }

        private void AddUsers(List<User> users, int i)
        {
            BSCEntities context = new BSCEntities();

            //int SBU_ID = Int32.Parse(RadTreeViewSBU.SelectedValue);

            List<User> anotherUsers = (from u in context.Users
                                       where u.SBU_FK == i
                                       select u).ToList();
            //users.Concat(anotherUsers);
            foreach (User u in anotherUsers)
            {
                users.Add(u);
            }
        }

        protected void RadComboBoxEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            BSCEntities context = new BSCEntities();

            int userID = Int32.Parse(RadComboBoxEmployee.SelectedValue);

            User user = (from u in context.Users
                         where u.ID == userID
                         select u).SingleOrDefault();

            List<User> users = new List<User>();
            users.Add(user);
            RadListBoxFiltered.DataSource = RemoveAlreadySelectedUsers(users, RadListBoxSelectedEmployees.Items);
            RadListBoxFiltered.DataValueField = "ID";
            RadListBoxFiltered.DataTextField = "Fullname";
            RadListBoxFiltered.DataBind();

            CheckAll();
        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {
            foreach (RadListBoxItem i in RadListBoxFiltered.Items)
            {
                if (i.Checked == true && !AlreadyExist(i.Value))
                {
                    RadListBoxItem ni = new RadListBoxItem();
                    ni.Value = i.Value;
                    ni.Text = i.Text;
                    RadListBoxSelected.Items.Add(ni);
                }
            }

            //RadListBoxFiltered.Items.Clear();

            //RadListBoxItemCollection notRemove = new RadListBoxItemCollection(;
            //foreach (RadListBoxItem i in RadListBoxFiltered.Items)
            //{
            //    if (!i.Checked)
            //        i.Enabled = false;
            //}

            //RadListBoxFiltered.Items[0]. .Clear();
            //RadListBoxFiltered.Items = notRemove;
        }

        private bool AlreadyExist(string userID)
        {
            //bool exists = false;
            for (int i = 0; i < RadListBoxSelected.Items.Count; ++i)
            {
                if (RadListBoxSelected.Items[i].Value == userID)
                    return true;
            }

            return false;
        }

        protected void CheckBoxInferiorSBUs_CheckedChanged(object sender, EventArgs e)
        {
            PopulateAccordingSBU();
        }

        protected void RadButton2_Click(object sender, EventArgs e)
        {

        }

        public RadListBox RadListBoxSelectedEmployees
        {
            get
            {
                return RadListBoxSelected;
            }
        }

        protected void bb_Click(object sender, EventArgs e)
        {
            PlaceHolderSBU.Visible = true; //ShowSBU();
        }

        protected void RadComboBoxEmployee_Load(object sender, EventArgs e)
        {
            if (RadTreeViewSBU.Nodes.Count > 0)
            {
                RadTreeViewSBU.Nodes[0].Expanded = true;
            }
        }

        public event EventHandler ActionButtonClicked;
        protected void RadButtonConfirm_Click(object sender, EventArgs e)
        {
            if (ActionButtonClicked != null)
            {
                ActionButtonClicked(sender, e);
            }
        }

        public string ActionButtonName
        {
            get
            {
                return RadButtonConfirm.Text;
            }
            set
            {
                RadButtonConfirm.Text = value;
            }
        }
    }
}